package org.e_gold.safe.acc.impl;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.log4j.Logger;
import org.e_gold.model.UserAccount;
import org.e_gold.safe.acc.TSAService;
import org.e_gold.util.DateUtil;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;

@Service
public class TSAServiceImpl extends TSATransferAbstract implements TSAService {

    private static final Logger log = Logger.getLogger(TSAServiceImpl.class);

    @Override
    public boolean transfer(Float money, UserAccount user, Integer... account) throws Exception {

        if (user != null && money != null && money >= 0.1f) {
            log.info("::::::::::::::::::::::::::::::::::::::::");
            log.info(":: Start Transfer : " + DateUtil.getDate());
            log.info("::::::::::::::::::::::::::::::::::::::::");
            Document transferPage = authenticatedTransferPage(user);
            if (transferPage == null || transferPage.getElementById("ctl00_main_TransferFunds_txtAmount") == null) {
                // do login
                try {
                    login(user);
                    transferPage = authenticatedTransferPage(user);
                } catch (Exception e) {
                    log.error(ToStringBuilder.reflectionToString(e, ToStringStyle.SHORT_PREFIX_STYLE));
                }
            }
            // do transfer
            transfer(money, transferPage, user);

            log.info("::::::::::::::::::::::::::::::::::::::::");
            log.info(":: End Transfer : " + DateUtil.getDate());
            log.info("::::::::::::::::::::::::::::::::::::::::");
            return true;
        }
        return false;
    }

}
